package com.azzcs.coyote;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Author: wzg
 * @Date: 2021/1/9 上午7:08
 */
public class Request {
    private String method;
    private String url;
    private String host;
    private String context;
    private String wrapper="";
    private InputStream inputStream;

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getContext() {
        return context;
    }

    public void setContext(String context) {
        this.context = context;
    }

    public String getWrapper() {
        return wrapper;
    }

    public void setWrapper(String wrapper) {
        this.wrapper = wrapper;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;

        // 从输入流中获取请求信息
        int count = 0;
        while (count == 0) {
            count = inputStream.available();
        }

        byte[] bytes = new byte[count];
        inputStream.read(bytes);

        String inputStr = new String(bytes);
        // 获取第一行请求头信息
        String firstLineStr = inputStr.split("\\n")[0];  // GET / HTTP/1.1
        // host
        String[] headers = inputStr.split("\\n");
        for (String header : headers) {
            if(header.contains("Host")){
                this.host = header.split(":")[1];
                break;
            }
        }
//        String secondLineStr = inputStr.split("\\n")[1];  // GET / HTTP/1.1
//        this.host = secondLineStr.split(":")[1];

        String[] strings = firstLineStr.split(" ");

        this.method = strings[0];
        this.url = strings[1];
        String[] split = this.url.split("/");
        this.context = split[1];
        for (int i = 2; i < split.length; i++) {
            this.wrapper += "/"+split[i];
        }


    }
}
